package com.ds.bintree.LeetCode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author Dianzhi Han
 * @version 1.0
 * @description: TODO
 * @date 2022/9/9 22:47
 */
public class Num102_LevelOrder {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret=new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();//辅助队列
        if(root==null){
            return ret;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            List<Integer> list=new LinkedList<>();
            int size=queue.size();
            for (int i=0;i<size;i++) {
                TreeNode temp=queue.poll();
                list.add(temp.val);
                if (temp.left!=null){
                    queue.offer(temp.left);
                }
                if (temp.right!=null){
                    queue.offer(temp.right);
                }
            }
            ret.add(list);
        }
        return ret;
    }
}
